import {createRouter, createWebHistory,createWebHashHistory} from "vue-router"
import Main from "../views/Main.vue"

const routes = [
    {
        path: '/',
        component: Main
    },
    {
        path: '/manager',
        component: () => import('../views/Manager.vue'),
    },
    {
        path: '/article/:id',
        component: () => import('../views/Article.vue'),
    },
    {
        path: '/login',
        component: () => import('../views/Login.vue')
    },
    {
        path: '/edit/:id',
        component: () => import('../views/Edit.vue')
    }
]

const router = createRouter({
    history:createWebHistory(),
    routes
})

router.beforeEach((to,from,next) =>{
    if (to.path == "/manager" || to.path == "/edit/:id"){
        if (localStorage.getItem("isLogin") == "true" && localStorage.getItem("token") != ""){
            next()
        }else{
            next("/login")
        }
    }else{
        next()
    }
})


export default router